//
//  BaseAdjustButton.m
//  LoveIN
//
//  Created by wangpanpan on 2019/12/12.
//  Copyright © 2019 bst. All rights reserved.
//

#import "BaseAdjustButton.h"

@implementation BaseAdjustButton
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    return self.wppImageRect;
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    return self.wppTitleRect;
}
//取消高亮
- (void)setHighlighted:(BOOL)highlighted
{
    
}


- (void)begin
{
    UIFont *titleFont = [UIFont systemFontOfSize:self.fontSize weight:self.fontWeight];
    CGFloat titleWidth = [self.title sizeWithFont:titleFont].width;
    CGFloat titleHeight = self.fontSize + 1*ADAPTER_WIDTH;
    
    CGFloat orinX = self.marginLeft,orinY = self.marginTop;
    CGFloat buttonHeight,buttonWidth;
    
    CGRect imageFrame,titleFrame;
    switch (_type) {
        case layoutTypeHorizontalLeftImageRightTitle:
        {
            buttonHeight = _marginTop + _imageHeight + _marginBottom;
            imageFrame = CGRectMake(orinX, _marginTop, _imageWidth, _imageHeight);
            orinX = _marginLeft + _imageWidth + _marginItemX;
            titleFrame = CGRectMake(orinX, (buttonHeight - titleHeight) * 0.5, titleWidth, titleHeight);
            buttonWidth = orinX + titleWidth + _marginRight;
        }
            break;
        case layoutTypeHorizontalLeftTitleRightImage:
        {
            buttonHeight = _marginTop + _imageHeight + _marginBottom;
            titleFrame = CGRectMake(orinX, (buttonHeight - titleHeight) * 0.5, titleWidth, titleHeight);
            orinX = _marginLeft + titleWidth + _marginItemX;
            imageFrame = CGRectMake(orinX, _marginTop, _imageWidth, _imageHeight);
            buttonWidth = orinX + _imageWidth + _marginRight;
        }
            break;
        case layoutTypeVerticalTopImageBottomTitle:
        {
            buttonWidth = _marginLeft + titleWidth + _marginRight;
            imageFrame = CGRectMake((buttonWidth - _imageWidth) * 0.5, _marginTop, _imageWidth, _imageHeight);
            orinY = _marginTop + _imageHeight + _marginItemY;
            titleFrame = CGRectMake(orinX, orinY, titleWidth, titleHeight);
            buttonHeight = orinY + titleHeight + _marginBottom;
        }
            break;
        case layoutTypeVerticalTopTitleBottomImage:
        {
            buttonWidth = _marginLeft + titleWidth + _marginRight;
            titleFrame = CGRectMake(orinX, orinY, titleWidth, titleHeight);
            orinY = _marginTop + titleHeight + _marginItemY;
            imageFrame = CGRectMake((buttonWidth - _imageWidth) * 0.5, orinY, _imageWidth, _imageHeight);
            buttonHeight = orinY + _imageHeight + _marginBottom;
        }
            break;
        case layoutTypeOnlyImage:
        {
            buttonWidth = _marginLeft + _imageWidth + _marginRight;
            buttonHeight = _marginTop + _imageHeight + _marginBottom;
            titleFrame = CGRectZero;
            imageFrame = CGRectMake(_marginLeft, _marginTop, _imageWidth, _imageHeight);
        }
            break;
        default:
            break;
    }
    CGRect frame = CGRectMake(0, 0, buttonWidth, buttonHeight);
    
    self.wppImageRect = imageFrame;
    self.wppTitleRect = titleFrame;
    self.frame = frame;
    
    [self setTitle:_title forState:UIControlStateNormal];
    self.titleLabel.font = titleFont;
}
@end
